<template>
    <div ref="chart" style="width: 600px; height: 400px;"></div>
  </template>
  
  <script>
  import * as echarts from "echarts";
  
  export default {
    name: 'PieChart',
    props: {
      pieData: Array
    },
    mounted() {
      this.echartsInit(this.pieData);
    },
    methods: {
      echartsInit(data) {
        const myChart = echarts.init(this.$refs.chart); // 初始化容器
        const option = {
          tooltip: {
            trigger: 'item',
            formatter: '{a} <br/>{b} : {c} ({d}%)'
          },
          legend: {
            orient: 'vertical',
            left: 'left',
            data: data.map(item => item.name)
          },
          series: [{
            name: '数据项',
            type: 'pie',
            radius: '55%',
            center: ['50%', '60%'],
            data: data,
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }]
        };
        myChart.setOption(option);
      }
    },
    watch: {
      pieData(newData) {
        this.echartsInit(newData);
      }
    }
  }
  </script>